package com.fjwt.gz.service.activity.platform.acte027;

import cn.hutool.core.date.DateUtil;
import com.fjwt.gz.db.dto.ACTE027StatDTO;
import com.fjwt.gz.db.entity.AgentInfoEntity;
import com.fjwt.gz.db.entity.SysExportTaskEntity;
import com.fjwt.gz.db.vo.ACTE027StatVO;
import com.fjwt.gz.db.vo.AgentInfoVO;
import com.fjwt.gz.service.activity.AbstractExportReport;
import com.fjwt.gz.service.impl.AgentInfoService;
import com.fjwt.gz.service.impl.OldFansInfoService;
import com.fjwt.gz.service.impl.StatMemberNumService;
import com.fjwt.gz.service.mapper.StatMemberNumMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ------ 027老客唤醒活动导出报表
 */
@Slf4j
@Service
public class ACTE027ExportReport extends AbstractExportReport<ACTE027StatVO> {

    @Autowired
    private OldFansInfoService oldFansInfoService;
    @Autowired
    private StatMemberNumService statMemberNumService;
    @Autowired
    private StatMemberNumMapper statMemberNumMapper;
    @Autowired
    private AgentInfoService agentInfoService;

    private static final Integer SCORE_VALUE = 80;

    public static final String DETAIL = "detail";
    public static final String AGENT_RECALL_DETAILS = "agentRecallDetails";
    public static final String RECALL_DETAILS = "recallDetails";

    private static final Map<String, String> detailHeaderAliasMap = new HashMap<>();
    static {
        detailHeaderAliasMap.put("fansId", "客户id");
        detailHeaderAliasMap.put("realName", "姓名");
        detailHeaderAliasMap.put("phone", "手机号");
        detailHeaderAliasMap.put("agentNo4", "用户归属");
        detailHeaderAliasMap.put("agentName4", "机构名称");
        detailHeaderAliasMap.put("agentNo3", "区县机构名称");
        detailHeaderAliasMap.put("agentName3", "区县机构名称");
    }

    private static final Map<String, String> agentRecallDetailsHeaderAliasMap = new HashMap<>();
    static {
        agentRecallDetailsHeaderAliasMap.put("agentNo", "网点编码");
        agentRecallDetailsHeaderAliasMap.put("agentName", "网点名称");
        agentRecallDetailsHeaderAliasMap.put("pId", "网点归属区县编码");
        agentRecallDetailsHeaderAliasMap.put("pName", "区县机构");
        agentRecallDetailsHeaderAliasMap.put("totalResidueSyncMemberNum", "权益中心待召回会员");
        agentRecallDetailsHeaderAliasMap.put("totalSyncMemberNum", "已召回会员");
        agentRecallDetailsHeaderAliasMap.put("activitySyncMemberNum", "焕活礼领取情况");
        agentRecallDetailsHeaderAliasMap.put("activitySyncMemberScoreNum", "领取福分数");
    }

    private static final Map<String, String> recallDetailsHeaderAliasMap = new HashMap<>();
    static {
        recallDetailsHeaderAliasMap.put("agentName", "单位");
        recallDetailsHeaderAliasMap.put("nextAgentNum", "网点数");
        recallDetailsHeaderAliasMap.put("totalResidueSyncMemberNum", "权益中心待召回会员");
        recallDetailsHeaderAliasMap.put("totalSyncMemberNum", "已召回会员");
        recallDetailsHeaderAliasMap.put("totalSyncMemberNumAvg", "点均");
        recallDetailsHeaderAliasMap.put("totalSyncMemberNumSort", "排名");
        recallDetailsHeaderAliasMap.put("activitySyncMemberNum", "焕活礼领取情况");
        recallDetailsHeaderAliasMap.put("activitySyncMemberScoreNum", "焕活礼发放福分");
    }


    @Override
    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
        ACTE027StatDTO acte027StatDTO = getRequestParam(sysExportTaskEntity.getRequestParam(), ACTE027StatDTO.class);
        if (DETAIL.equals(acte027StatDTO.getExportType())) {
            List<ACTE027StatVO> list = oldFansInfoService.getActe027Export(acte027StatDTO);
            return createExcelAndUploadOssUrl(list, detailHeaderAliasMap, sysExportTaskEntity);
        }else if (AGENT_RECALL_DETAILS.equals(acte027StatDTO.getExportType())) {
            acte027StatDTO.setStatDate(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.date(), -1)));
            List<ACTE027StatVO> list = statMemberNumMapper.getAgentRecallDetails(acte027StatDTO);

            for (ACTE027StatVO record : list) {
                statMemberNumService.setAgentName(record);
                // 总剩余同步旧会员数量
                record.setTotalResidueSyncMemberNum(record.getTotalOldMemberNum() - record.getTotalSyncMemberNum());
                record.setActivitySyncMemberScoreNum(record.getActivitySyncMemberNum() * SCORE_VALUE);
            }
            return createExcelAndUploadOssUrl(list, agentRecallDetailsHeaderAliasMap, sysExportTaskEntity);
        }else if (RECALL_DETAILS.equals(acte027StatDTO.getExportType())) {
            acte027StatDTO.setStatDate(DateUtil.beginOfDay(DateUtil.offsetDay(DateUtil.date(), -1)));
            acte027StatDTO.setTotalSyncMemberNumSortDesc(true);
            if (ObjectUtils.isNotEmpty(acte027StatDTO.getAgentNo())) {
                AgentInfoEntity agentInfo = agentInfoService.getAgentInfo(acte027StatDTO.getAgentNo());
                if (ObjectUtils.isNotEmpty(agentInfo)) {
                    if (agentInfo.getLevel() < 3) {
                        acte027StatDTO.setAgentNo(null);
                        List<AgentInfoVO> agentInfoVOList = agentInfoService.querySubAgentList(acte027StatDTO.getAgentNo());
                        List<String> collect = agentInfoVOList.stream().map(AgentInfoVO::getAgentNo).toList();
                        collect.add(null);
                        acte027StatDTO.setAgentNoList(collect);
                    }
                }
            }

            List<ACTE027StatVO> list = statMemberNumMapper.getAgentRecallDetails(acte027StatDTO);

            for (ACTE027StatVO record : list) {
                statMemberNumService.setAgentName(record);
                // 总剩余同步旧会员数量
                record.setTotalResidueSyncMemberNum(record.getTotalOldMemberNum() - record.getTotalSyncMemberNum());
                record.setActivitySyncMemberScoreNum(record.getActivitySyncMemberNum() * SCORE_VALUE);
                record.setTotalSyncMemberNumAvg(new BigDecimal(record.getTotalSyncMemberNum())
                        .divide(new BigDecimal(statMemberNumService.getDefaultValue(record.getNextAgentNum())), RoundingMode.HALF_UP));
            }
            return createExcelAndUploadOssUrl(list, recallDetailsHeaderAliasMap, sysExportTaskEntity);
        }

        return "";
    }
}
